﻿using RuoVea.Omi.Web.Template.Server.Language;
namespace RuoVea.Omi.Web.Template.Web.Controllers;

/// <summary>
/// 
/// </summary>
[Authorize(Roles = "None")]
public partial class DemoController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly DemoService _demoService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="demoService"></param> 
    public DemoController(DemoService demoService)
    {
        _demoService = demoService; 
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("[controller]/pages")]
    public async Task<RestfulResult<PageResult<AppDemo>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.upload_data_required);
        AppDemoPageParam? data = JsonConvert.DeserializeObject<AppDemoPageParam>(dataStr);
        data ??= new AppDemoPageParam { PageNo = 1, PageSize = 10 };

        var query = await _demoService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("[controller]/list")]
    public async Task<RestfulResult<PageResult<AppDemo>>> GetlistAsync()
    {
        var restful = await _demoService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("[controller]/add")]
    public async Task<RestfulResult> AddAsync([FromBody] AppDemo data)
    {
        var restful = await _demoService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("[controller]/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] AppDemo data)
    {
        var restful = await _demoService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("[controller]/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _demoService.DeleteAsync(data);
        return restful;
    }
}

